package com.cmc.cloud.cmclink.doc.controller.admin.si;

import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.service.si.BlnoSpiltCombineService;
import com.cmc.cloud.cmclink.doc.vo.blnospiltcombinevo.BlnoCombineVO;
import com.cmc.cloud.cmclink.doc.vo.blnospiltcombinevo.BlnoSpiltChildVO;
import com.cmc.cloud.cmclink.doc.vo.blnospiltcombinevo.BlnoSpiltCombinePageReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnospiltcombinevo.BlnoSpiltCombinePageRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnospiltcombinevo.BlnoSpiltRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnospiltcombinevo.BlnoSpiltVO;
import com.cmes.framework.biz.advice.annotations.UseResponsePropertyAdvice;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import com.cmes.framework.common.pojo.PageResult;

import com.cmc.cloud.cmclink.doc.entity.BlnoSpiltCombineDO;
import com.cmc.cloud.cmclink.doc.convert.BlnoSpiltCombineConvert;

/**
 * 提单拆并申请主前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 提单拆并申请主")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/spiltCombine")
public class BlnoSpiltCombineController {

    @Resource
    private BlnoSpiltCombineService blnoSpiltCombineService;

    @Operation(summary = "删除提单拆并申请主")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:splitCombineBlApply:delete')")
    public CommonResult<Void> deleteBlnoSpiltCombine(@PathVariable("id") Long id) {
        blnoSpiltCombineService.deleteBlnoSpiltCombine(id);
        return CommonResult.success();
    }

    @Operation(summary = "获得提单拆并申请主分页")
    @OperateLog(type = OperateTypeEnum.GET)
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermission({'doc:splitCombineBlApply:search','doc:splitCombineBlApprove:search'})")
    @UseResponsePropertyAdvice
    public CommonResult<PageResult<BlnoSpiltCombinePageRespVO>>
    getSpiltCombinePage(@RequestBody @Valid BlnoSpiltCombinePageReqVO pageReqVO) {
        return CommonResult.success(blnoSpiltCombineService.getSpiltCombinePage(pageReqVO));
    }

    @Operation(summary = "提单拆单")
    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping("/spilt")
    @PreAuthorize("@ss.hasPermission('doc:maintainBL:splitBl')")
    public CommonResult<Long> commitBlnoSpilt(@Valid @RequestBody BlnoSpiltVO blnoSpiltVO) {
        return CommonResult.success(blnoSpiltCombineService.commitBlnoSpiltNew(blnoSpiltVO));
    }

    @Operation(summary = "提单并单")
    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping("/combine")
    @PreAuthorize("@ss.hasPermission('doc:maintainBL:combineBl')")
    public CommonResult<Long> commitBlnoCombine(@Valid @RequestBody BlnoCombineVO blnoCombineVO) {
        return CommonResult.success(blnoSpiltCombineService.commitBlnoCombineNew(blnoCombineVO));
    }

    @Operation(summary = "根据提单ID 列表查询 待合并的 提单 货物,箱信息")
    @OperateLog(type = OperateTypeEnum.GET)
    @PostMapping("/getBeCombineData")
    public CommonResult<List<BlnoSpiltChildVO>> getBeCombineData(@Valid @RequestBody List<Long> blnoIds) {
        List<BlnoSpiltChildVO> beCombineData = blnoSpiltCombineService.getBeCombineData(blnoIds);
        return CommonResult.success(beCombineData);
    }

    @Operation(summary = "根据拆单记录主id  查询详情")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/getSpiltDetail/{id}")
    @PreAuthorize("@ss.hasPermission({'doc:splitCombineBlApply:preview','doc:splitCombineBlApprove:preview'})")
    public CommonResult<BlnoSpiltRespVO> getSpiltDetail(@PathVariable("id") Long id) {
        return CommonResult.success(blnoSpiltCombineService.getSpiltDetail(id));
    }

    @Operation(summary = "根据并单记录主id  查询详情")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/getCombineDetail/{id}")
    @PreAuthorize("@ss.hasPermission({'doc:splitCombineBlApply:preview','doc:splitCombineBlApprove:preview'})")
    public CommonResult<BlnoCombineVO> getCombineDetail(@PathVariable("id") Long id) {
        return CommonResult.success(blnoSpiltCombineService.getCombineDetail(id));
    }

    @Operation(summary = "通过提单拆单申请")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/approveSpilt/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> approveSpilt(@PathVariable("id") Long operationId, @RequestParam(required = false) String approveReason) {
        blnoSpiltCombineService.approveSpiltOrCombine(operationId, true, approveReason);
        return CommonResult.success();
    }


    @Operation(summary = "通过提单并单申请")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/approveCombine/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> approveCombine(@PathVariable("id") Long operationId, @RequestParam(required = false) String approveReason) {
        blnoSpiltCombineService.approveSpiltOrCombine(operationId, false, approveReason);
        return CommonResult.success();
    }

    @Operation(summary = "拒绝提单拆单申请")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/rejectSpilt/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> rejectSpilt(@PathVariable("id") Long operationId, @RequestParam String approveReason) {
        blnoSpiltCombineService.rejectSpiltOrCombine(operationId, true, approveReason);
        return CommonResult.success();
    }


    @Operation(summary = "拒绝提单并单申请")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/rejectCombine/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> rejectCombine(@PathVariable("id") Long operationId, @RequestParam String approveReason) {
        blnoSpiltCombineService.rejectSpiltOrCombine(operationId, false, approveReason);
        return CommonResult.success();
    }

    @Operation(summary = "拆单前数据校验")
    @OperateLog(type = OperateTypeEnum.OTHER)
    @PostMapping("/checkSpiltData/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Void> checkOriginalSpiltData(@PathVariable("id") Long blnoId) {
        blnoSpiltCombineService.checkOriginalSpiltData(blnoId);
        return CommonResult.success();
    }

    @Operation(summary = "并单前数据校验")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/checkCombineData")
    public CommonResult<Void> checkOriginalCombineData(@Valid @RequestBody List<Long> blnoIds) {
        blnoSpiltCombineService.checkOriginalCombineData(blnoIds);
        return CommonResult.success();
    }

}
